package cn.chinasyq.photoquan.photo;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.io.File;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.user.activity.LoginActivity;
import cn.chinasyq.photoquan.user.bean.GameOrder;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.chinasyq.photoquan.util.BitmapUtil;
import cn.chinasyq.photoquan.util.DialogUtil;
import cn.master.util.utils.ScreenUtil;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.models.response.handler.ResponseHandler;

/**
 * Created by 一搏 on 2015/12/19.
 */
public class GameParticipateActivity extends Activity implements View.OnClickListener, ResponseHandler.OnSucceedListener, ResponseHandler.OnNeedLoginListener, ResponseHandler.OnFailedListener, ResponseHandler.OnUploadingListener {
    private static final String TAG_UPLOAD = "upload";
    String uuid;
    Uri image;
    EditText et_title, et_name, et_info;
    private ImageView iv;
    private Dialog progress_Dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_participate);
        uuid = getIntent().getStringExtra("uuid");
        image = getIntent().getData();
        et_info = (EditText) findViewById(R.id.et_info);
        et_name = (EditText) findViewById(R.id.et_name);
        et_title = (EditText) findViewById(R.id.et_title);
        iv = (ImageView) findViewById(R.id.iv);
        iv.setLayoutParams(new LinearLayout.LayoutParams(ScreenUtil.getScreenWidth(this),ScreenUtil.getScreenWidth(this)));
        iv.setImageURI(image);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back:
                onBackPressed();
                break;
            case R.id.button4:
                upLoad();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        DialogUtil.showAskDialog(GameParticipateActivity.this, "提示", "是否确认放弃参赛？", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        }, false);
    }

    public void upLoad() {
        if (progress_Dialog == null) {
            progress_Dialog= ProgressDialog.show(this,"","上传数据中",true,false);
        }else {
            progress_Dialog.show();
        }
        ResponseHandler handler = new ResponseHandler(TAG_UPLOAD);
        handler.setOnSucceedListener(this);
        handler.setOnNeedLoginListener(this);
        handler.setOnFailedListener(this);
        handler.setOnUploadingListener(this);
        UserMode.uploadGamePhoto(GameParticipateActivity.this, new File(BitmapUtil.convertUri2FilePath(this, image)), uuid, et_name.getText().toString(), et_title.getText().toString(), et_info.getText().toString(), handler);
    }

    @Override
    public void onSucceed(String tag, boolean isCache, Object data) {
        if (progress_Dialog.isShowing()){
            progress_Dialog.dismiss();
        }
        GameOrder order = (GameOrder) data;
        Intent intent = new Intent(GameParticipateActivity.this,GameParticipateInfoActivity.class);
        intent.putExtra("uuid",order.getUuid());
        startActivity(intent);
        finish();
    }

    @Override
    public void onNeedLogin(String tag) {
        startActivity(new Intent(this, LoginActivity.class));
    }

    @Override
    public void onFailed(String tag, ResponseHandler.FailedStatus status, int resultCode, String msg) {
        if (progress_Dialog.isShowing()){
            progress_Dialog.dismiss();
        }
        if (msg == null) {
            switch (status) {
                case DATA_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.tip_data_error);
                    break;
                case NETWORK_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.servererrortips);
                    break;
            }
        } else {
            ToastUtil.show(getApplicationContext(), msg);
        }
    }

    @Override
    public void onUploading(String tag, int progress) {

    }
}
